<template>
  <div>
    <h1>讲师列表</h1>
    <!-- 模糊查询选择 -->
    <div align="center">
      <el-form :inline="true" :model="courseQuery" class="demo-form-inline">
        <el-form-item label="课程标题">
          <el-input
            v-model="courseQuery.title"
            placeholder="课程标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="主讲教师"
          ><el-select
            v-model="courseQuery.teacherId"
            clearable
            placeholder="选择讲师"
          >
            <el-option
              v-for="teacher in teacherList"
              :key="teacher.id"
              :label="teacher.name"
              :value="teacher.id"
            >
            </el-option> </el-select
        ></el-form-item>

        <el-form-item label="起始时间">
          <el-date-picker
            v-model="courseQuery.beginTime"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间"
          ><el-date-picker
            v-model="courseQuery.endTime"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="condition_list">查询</el-button>
          <el-button type="info" @click="reset_condition">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <br />

    <!-- 讲师表格 -->
    <el-table :data="courseList" style="width: 100%">
      <el-table-column label="序号" width="180">
        <template slot-scope="scope">
          {{ scope.$index + 1 + (currentPage - 1) * limit }}
        </template>
      </el-table-column>
      <el-table-column prop="title" label="课程标题" width="180">
      </el-table-column>
      <el-table-column label="主讲教师" width="180"  prop="teacherId">
          <template slot-scope="scope">
          {{get_teacher_name_by_id( scope.row.teacherId)}}
        </template>
      </el-table-column>

      <el-table-column prop="status" label="发布状态" width="180">
      </el-table-column>
      <el-table-column prop="price" label="售价(/元)" width="180">
      </el-table-column>
      <el-table-column prop="updateTime" label="更新时间"> </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            plain
            @click="handleEdit(scope.$index, scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <br />
    <!--            分页-->

    <div align="center">
      <div class="block">
        <el-pagination
          background
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage"
          :page-size="limit"
          layout="total,prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <br />
    <br />
  </div>
</template>
<script>
import course from "@/api/edu/course";
import teacher from "@/api/edu/teacher";
export default {
  data() {
    return {
      currentPage: 1, //当前页
      limit: 10, //每页记录数
      total: 0, //总记录数
      //数据展示
      courseList: [
        {
          id: null,
          teacherId: null,
          subjectId: null,
          subjectParentId: null,
          title: null,
          price: null,
          lessonNum: null,
          cover: null,
          buyCount: null,
          view: null,
          version: null,
          status: null,
          updateTime: null,
        },
      ],
      //讲师姓名
      teacherList: [
        {
          id: null,
          name: null,
        },
      ],
      //查询数据
      courseQuery: {
        beginTime: null,
        endTime: null,
        status: null,
        teacherId: null,
        title: null,
      },
    };
  },
  methods: {
    //查询列表分页
    page_list(current, size, courseQuery) {
      course.get_list(current, size, courseQuery).then((Response) => {
        this.courseList = Response.data.courseList;
        this.total = Response.data.total;
      });
    },
    //分页切换
    handleCurrentChange(val = 1) {
      this.page_list(val, this.limit, this.courseQuery);
    },
    //条件
    condition_list() {
      this.page_list(1, this.limit, this.courseQuery);
    },
    //重置
    reset_condition() {
      this.courseQuery.title = null;
      this.courseQuery.teacherId = null;
      this.courseQuery.status = null;
      this.courseQuery.beginTime = null;
      this.courseQuery.endTime = null;
      this.page_list(1, this.limit);
    },
    //删除
    handleDelete(index, row) {
      console.log(index);
      console.log(row.id);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          course.delete_course(row.id).then((response) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.courseList.splice(index, 1);
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //修改
    handleEdit(index, row) {
      this.$router.push("/course/info/" + row.id);
    },
    //获取讲师姓名 id
    get_teacher_name_list() {
      teacher.get_all_teacher().then((Response) => {
        this.teacherList = Response.data.teacherList;
      });
    },
    get_teacher_name_by_id(id){
        for(var i=0;i<this.teacherList.length;i++){
            if(id==this.teacherList[i].id){
                return this.teacherList[i].name;
            }
        }
    }
  },
  mounted() {
    this.page_list(1, this.limit);
    this.get_teacher_name_list();
  },
};
</script>

<style>
</style>